/*-*
 * FILENAME  :
 *    $HeadURL$
 *
 * STATUS  :
 *    $Revision$
 *
 *    $Author$
 *    $Date$
 *  
 *    
 * Copyright (c) 2011 Gonalge Ltda. All rights reserved.
 *
 ****************************************************************/

package org.gonalge.sudoku.negocio;

import java.io.Serializable;

import org.apache.commons.logging.LogFactory;

/**
 * 
 * @author Geremias Gonzalez
 */
public class Matriz implements Serializable {
    /**
     * 
     */
    private static final long serialVersionUID = 3897540210458213346L;

    private final int fila;

    private final int columna;

    private final int matriz[][];

    private final int dimCuadrante;

    public Matriz() {
        fila = 9;
        columna = 9;
        dimCuadrante = (int) Math.sqrt(fila);
        matriz = new int[columna][fila];
    }

    public Matriz(int fi, int col) {
        fila = fi;
        columna = col;
        dimCuadrante = (int) Math.sqrt(fila);
        matriz = new int[columna][fila];
    }

    public void limpiar(int i, int j) {
        matriz[i][j] = 0;
    }

    public boolean insertar(int i, int j, int e) {

        if (verificarColumna(j, e) && verificarFila(i, e))
            if (verificarSubMatriz(i, j, e)) {
                if (e < fila + 1 && e > 0)
                    matriz[i][j] = e;
                return true;
            }

        return false;
    }

    public int Consultar(int i, int j) {
        return matriz[i][j];
    }

    public boolean verificarFila(int f, int e) {
        int c = 0;
        boolean sw = true;
        while (c < fila && sw) {
            if (matriz[f][c] == e)
                sw = false;
            c++;
        }
        return sw;
    }

    public boolean verificarColumna(int c, int e) {
        int f = 0;
        boolean sw = true;
        while (f < fila && sw) {
            if (matriz[f][c] == e)
                sw = false;
            f++;
        }
        return sw;
    }

    public boolean verificarSubMatriz(int f, int c, int e) {
        int i = 0, j = 0;
        i = (f / dimCuadrante) * dimCuadrante;
        j = (c / dimCuadrante) * dimCuadrante;

        int nf = i + dimCuadrante;
        int nc = j + dimCuadrante;

        boolean sw = true;
        while (i < nf && sw) {
            j = +nc - dimCuadrante;
            while (j < nc && sw) {
                if (matriz[i][j] == e)
                    sw = false;
                j++;
            }
            i++;
        }
        return sw;
    }

    public boolean diagonales(int e) {

        int i = 0, j = columna - 1;
        boolean sw = true;
        while (i < fila && sw) {
            if (matriz[i][i] == e || matriz[i][j] == e)
                sw = false;
            i++;
            j--;
        }
        return sw;
    }

    public void imprimir() {
        String print = "";
        for (int i = 0; i < fila; i++) {
            for (int j = 0; j < columna; j++) {
                if (matriz[i][j] < 10)
                    print += " " + matriz[i][j] + "    ";
                else
                    print += (matriz[i][j] + "    ");
            }
            print += "\n";
        }
        LogFactory.getLog(getClass()).info(print + "\n");
    }

    public int getFila() {
        return fila;
    }

    public int getColumna() {
        return columna;
    }

    public int getDimCuadrante() {
        return dimCuadrante;
    }
}
